package com.tsingteng.cosfun.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.utils.ToastUtils;

import java.util.List;

/**
 * 描述 :底部列表弹框
 * 创建人: 肖利.
 * 时间:2018/8/15
 * qq(872179966)
 */

public class BottomListDialog {

    private Context context;
    private Dialog dialog;
    private List<String> listContent;
    private TextView tvBottom;
    private RecyclerView mLv;
    private ListAdapter listAdapter;
    private OnClickBottomListListener onClickBottomListListener;
    private RelativeLayout mRlBottom;

    public BottomListDialog(Context context) {
        this.context = context;
        initView();
    }

    private void initView() {
        dialog = new Dialog(context,R.style.dialog);
        dialog.setContentView(R.layout.dialog_list_bottom);
        tvBottom = dialog.findViewById(R.id.tv_bottom);
        mRlBottom = dialog.findViewById(R.id.rl_bottom);
        mLv = dialog.findViewById(R.id.lv_list_view);
        mLv.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        listAdapter = new ListAdapter();
        mLv.setAdapter(listAdapter);
        listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (onClickBottomListListener!= null) {
                    onClickBottomListListener.onClickItem(adapter,view,position,listContent);
                    dialog.dismiss();
                }
            }
        });
        mRlBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickBottomListListener!= null) {
                    onClickBottomListListener.onClickBottom();
                    dialog.dismiss();
                }
            }
        });
    }


    public void show() {
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setWindowAnimations(R.style.dialogWindowAnim);
        dialog.show();


    }

    public void dissMiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();

        }
    }


    class ListAdapter extends BaseQuickAdapter<String,BaseViewHolder> {

        public ListAdapter() {
            super(R.layout.item_list_bottom);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            TextView textView = helper.getView(R.id.tv_item);
            textView.setText(item);
        }


    }


    public interface OnClickBottomListListener{
            void onClickItem(BaseQuickAdapter adapter, View view, int position,List<String> list);
            void onClickBottom();
    }


    public void setOnClickBottomListListener(OnClickBottomListListener onClickBottomListListener) {
        this.onClickBottomListListener= onClickBottomListListener;
    }


    public void setList(List<String> list) {
        this.listContent = list;
        listAdapter.getData().clear();
        listAdapter.addData(listContent);
    }



}
